package com.tjsinfo.tjsoa.mail.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.JSONObject;

import com.tjsinfo.tjsoa.common.PageController;
import com.tjsinfo.tjsoa.common.SessionUser;
import com.tjsinfo.tjsoa.mail.bo.ITjsEmailFolderBo;
import com.tjsinfo.tjsoa.mail.bo.ITjsemailReceivedBo;
import com.tjsinfo.tjsoa.mail.vo.TjsEmailFolder;
import com.tjsinfo.tjsoa.mail.vo.TjsemailReceived;
/**
 * 收件箱列表action
 * @author wangxiaowei
 *
 */
public class EmailWeiReadReceivedAction extends Action {
	private ITjsemailReceivedBo tjsemailReceivedBo;
	private ITjsEmailFolderBo tjsEmailFolderBo;
	
	public ITjsemailReceivedBo getTjsemailReceivedBo() {
		return tjsemailReceivedBo;
	}

	public void setTjsemailReceivedBo(ITjsemailReceivedBo tjsemailReceivedBo) {
		this.tjsemailReceivedBo = tjsemailReceivedBo;
	}

	public ITjsEmailFolderBo getTjsEmailFolderBo() {
		return tjsEmailFolderBo;
	}

	public void setTjsEmailFolderBo(ITjsEmailFolderBo tjsEmailFolderBo) {
		this.tjsEmailFolderBo = tjsEmailFolderBo;
	}

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	        throws Exception {
		//TjsemailReceived tjsemailReceived2=new TjsemailReceived();
		//List list3 = new ArrayList();
		List<TjsemailReceived> list =	tjsemailReceivedBo.selectNewUserId(String.valueOf(SessionUser.getUserId(request)),1);//收件列表
		//Integer count=tjsemailReceivedBo.count(1);
		Set<TjsemailReceived> set = new HashSet<TjsemailReceived>(list);
		list = new ArrayList<TjsemailReceived>(set);
		if ("top".equals(request.getParameter("_order"))) {
		    request.setAttribute("_order", "top");
			Comparator comp = new Comparator(){
		          public int compare(Object o1,Object o2) {
		        	  TjsemailReceived p1=(TjsemailReceived)o1;
		        	  TjsemailReceived p2=(TjsemailReceived)o2; 
		              	if (p1.getSortid() > p2.getSortid()) {
		              		return 1;
						} else {
							return 0;
						}
		                 
		             }
		        };
		        Collections.sort(list, comp);
		}else {
			Comparator comp = new Comparator(){
		          public int compare(Object o1,Object o2) {
		        	  TjsemailReceived p1=(TjsemailReceived)o1;
		        	  TjsemailReceived p2=(TjsemailReceived)o2; 
		              	if (p1.getSortid() < p2.getSortid()) {
		              		return 1;
						} else {
							return 0;
						}
		                 
		             }
		        };
		        Collections.sort(list, comp);
		}
		if (!"".equals(request.getParameter("sendUserName")) && request.getParameter("sendUserName")!=null) {
			ArrayList<TjsemailReceived> list1 = new ArrayList<TjsemailReceived>();
			String sendUserName = request.getParameter("sendUserName");
			sendUserName= new String(sendUserName.getBytes("iso-8859-1"), "utf-8");
			for (Iterator iterator = list.iterator(); iterator.hasNext();) {
				TjsemailReceived received = (TjsemailReceived) iterator.next();
				if (received.getTjsmail().getTjsmailUsername().indexOf(sendUserName)!=-1) {
					list1.add(received);
				}
			}
			list = list1;
		    request.setAttribute("sendUserName", sendUserName);
		}
		if (!"".equals(request.getParameter("sendTitle")) && request.getParameter("sendTitle")!=null) {
			ArrayList<TjsemailReceived> list1 = new ArrayList<TjsemailReceived>();
			String sendTitle = request.getParameter("sendTitle");
			sendTitle= new String(sendTitle.getBytes("iso-8859-1"), "utf-8");
			for (Iterator iterator = list.iterator(); iterator.hasNext();) {
				TjsemailReceived received = (TjsemailReceived) iterator.next();
				if (received.getTjsmail().getTjsmailTitle().indexOf(sendTitle)!=-1) {
					list1.add(received);
				}
			}
			list = list1;
		    request.setAttribute("sendTitle", sendTitle);
		}	
		Integer contentspage = null;
		String start = null;
		
		 PageController pageController = new PageController();
		    if(request.getParameter("page") != null && request.getParameter("start") == null )
			{
				pageController.setPageController(list);
				pageController.setMaxPageRows(13);
				list = (ArrayList) pageController.getPageContents(Integer.parseInt(request.getParameter("page") ));
		
			}
		    else if (request.getParameter("page") != null) {
				contentspage = Integer.parseInt(request.getParameter("page"));
				start = request.getParameter("start");
				pageController.setPageController(list);
				pageController.setMaxPageRows(13);
				if (start.equals("up")) {
					list = (ArrayList) pageController
							.getPageContents(contentspage - 1);
				} else if(start.equals("down")) {
					list = (ArrayList) pageController
							.getPageContents(contentspage + 1);
				}
				else if(start.equals("first"))
				{
					list = (ArrayList) pageController
					.getPageContents(1);
				}
				else if(start.equals("last"))
				{
					list = (ArrayList) pageController
					.getPageContents(pageController.getPageCount());
				}
			} else {
				pageController.setPageController(list);
				pageController.setMaxPageRows(13);
				list = (ArrayList) pageController.getPageContents(1);
			}
		request.setAttribute("pageController",pageController);
		request.setAttribute("list", list);
		//List<TjsEmailFolder> list2 = tjsEmailFolderBo.selectByUserId(SessionUser.getUserId(request));
		//request.setAttribute("foldlist", list2);
		//String recstart1 = request.getParameter("recstart");
		request.setAttribute("count", tjsemailReceivedBo.count(String.valueOf(SessionUser.getUserId(request)),1));
		return mapping.findForward("ok");
	}
}
